# -*- coding: utf-8 -*-

import sys

sys.path.append('../')

from config import *
from boss_config import *


class BOSS():
    def __init__(self, post_headers):
        self.post_headers = post_headers
        self.login()

    def login(self):
        url = 'https://www.zhipin.com/?ka=header-home-logo'

        r = requests.get(url, headers = login_header)

        self.cookies = r.cookies
        self.begin()

    def begin(self):
        for keyword in ['证券经纪人', '英语教师', 'java工程师', '前端工程师', '理财顾问']:
            for city, code in city_codes.items():
                url = 'https://www.zhipin.com/c' + code + '/h_' + code + '/'

                for page in range(1, 2):
                    time.sleep(5)
                    print(url)
                    r = requests.get(url,
                                     params = {
                                         "query": keyword,
                                         "page": page,
                                         "ka": 'page-next'
                                     },
                                     timeout = 10,
                                     cookies = self.cookies,
                                     headers = self.post_headers, verify = False)

                    if r.text.find('由于您当前网络访问页面过于频繁') != -1:
                        print('账号已被限制')
                        continue

                    soup = BeautifulSoup(r.text)

                    for e in soup.select('.job-list ul li'):
                        print(e.find(class_ = 'job-title').text,
                              e.find(class_ = 'info-company').find(class_ = 'name').get_text())

    def scrapy_company_info(self):
        pass


BOSS(post_headers)
